<template>
    <div class="app-container">
      <el-form label-width="120px" :model="semesters" :rules="formRules" ref="dataForm">
        <el-form-item label="学期名称" prop="name">
          <el-input v-model="semesters.name"/>
        </el-form-item>
        <div style="display: flex;">
            <el-form-item label="开始日期" prop="startDate">
                <el-date-picker
                    v-model="semesters.startDate"
                    type="datetime"
                    format="yyyy-MM-dd"
                    placeholder="选择开始日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="结束日期" prop="endDate">
                <el-date-picker
                    v-model="semesters.endDate"
                    type="datetime"
                    format="yyyy-MM-dd"
                    placeholder="选择结束日期">
                </el-date-picker>
            </el-form-item>
        </div>
        <el-form-item>
          <el-button
            :disabled="saveBtnDisabled"
            type="primary"
            @click="saveOrUpdate">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
</template>
  <script>
  import semesters from '@/api/cms/semesters'
  import moment from 'moment';  
  export default {
    data() {
      return {
        // 存放实体信息
        semesters: {
            startDate: '',
            name: '',
            endDate: ''
        },
        saveBtnDisabled: false, // 保存按钮是否禁用
        options: undefined,
        formRules: {
            name: [
                { required: true, message: '请输入姓名', trigger: 'blur' }
            ],
            startDate: [
                { required: true, message: '请输入开始时间', trigger: 'blur' }
            ],
            endDate: [
                { required: true, message: '请输入结束时间', trigger: 'blur' }
            ]
        }
      }
    },
    watch: {
      $route(to, from) { // 监听路由变化方式
        this.init()
      }
    },
    created() {
      this.init()
    },
    methods: {
      init() {
        // 判断是否有id
        if (this.$route.params && this.$route.params.id) {
          const id = this.$route.params.id
          this.getInfo(id)
        } else {
            // 做添加 清空表单
            this.semesters =  {
                startDate: '',
                name: '',
                endDate: ''
            }
        }
      },
      // 根据id查询方法
      getInfo(id) {
        console.log("********")
        semesters.getInfoById(id)
          .then(response => {
            console.log(response);
            this.semesters = response.data.item
          })
      },
      saveOrUpdate() {
        this.$refs.dataForm.validate(valid => {
            // console.log(valid)
            if (valid) {
                // console.log('通过')
            // 表单验证通过，执行提交操作
            this.saveBtnDisabled = true
            // 判断saveBtnDisabled是否有id 修改或添加
                if (!this.semesters.id) {
                // 添加
                this.saveData()
                } else {
                this.updateData()
                }
            } else {
            // 表单验证失败，输出错误信息
            // console.log('不通过')
                this.$message.error('请正确输入表单');
            }
        })
      },
      // 修改方法
      updateData() {
        semesters.updateInfo(this.semesters)
          .then(response => {
            this.$message({
              type: 'success',
              message: '修改成功!'
            })
            this.$router.push({ path: '/semesters/table' })
          }).catch(response => {
            this.$message({
              type: 'error',
              message: '修改失败'
            })
          })
      },
      // 保存
      saveData() {
        semesters.addInfo(this.semesters)
          .then(response => { // 添加成功
            this.$message({
              type: 'success',
              message: '保存成功!'
            })
            // 路由跳转
            this.$router.push({ path: '/semesters/table' })
          }).catch(response => {
            this.$message({
              type: 'error',
              message: '保存失败'
            })
          })
      },
        // 时间变化触发事件
        handleChange(value) {
            this.semesters.startDate = moment(value[0]).format('yyyy-MM-dd HH:mm:ss');
            this.semesters.endDate = moment(value[1]).format('yyyy-MM-dd HH:mm:ss');
            // this.semesters.startDate = value[0];
            // this.semesters.endDate = value[1];
            console.log(this.semesters);
        }
    }
  }
  </script>
  